package com.smart.village.big.screen.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.smart.village.big.screen.domain.*;
import com.smart.village.big.screen.mapper.StudyFilesMapper;
import com.smart.village.big.screen.service.StudyFilesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
public class StudyFilesServiceImpl implements StudyFilesService {
    @Autowired
    private StudyFilesMapper studyFilesMapper;

    @Override
    public StudyFiles getStudyFilesDetail(String studyFileSerialNumber,String openId) {
        studyFilesMapper.addViewCount(studyFileSerialNumber);
        StudyFiles studyFiles = studyFilesMapper.getStudyFilesDetail(studyFileSerialNumber);
        if(!ObjectUtils.isEmpty(studyFiles)){
            List<JSONObject> images = studyFilesMapper.getStudyFilesDetailPictureList(studyFileSerialNumber);
           List<JSONObject> catalogList = Lists.newArrayList();
           if(!ObjectUtils.isEmpty(images)){
               JSONObject jsonObject = new JSONObject();
               jsonObject.put("images", images);
               jsonObject.put("title", studyFiles.getStudyFilesTitle());
               jsonObject.put("pages", images.size());
               jsonObject.put("isRead", false);
               jsonObject.put("id", studyFileSerialNumber);
               catalogList.add(jsonObject);
           }
           studyFiles.setCatalogList(catalogList);
        }
        if(!ObjectUtils.isEmpty(openId)){
            List<PersonalBrowsing> personalBrowsing = studyFilesMapper.getPersonalBrowsing(openId,studyFileSerialNumber);
            studyFiles.setPersonalBrowsing(personalBrowsing);
        }
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (null != studyFiles.getStudyFilesPublishDatetime() && !Objects.equals(studyFiles.getStudyFilesPublishDatetime(), "") &&(new Date().getTime() - dateFormat.parse(studyFiles.getStudyFilesPublishDatetime()).getTime()) / (24 * 60 * 60 * 1000) <= 3) {
                studyFiles.setIfNew("1");
            } else {
                studyFiles.setIfNew("0");
            }
        } catch (ParseException e) {
            e.printStackTrace();
            studyFiles.setIfNew("0");
        }
        return studyFiles;
    }

    @Override
    public List<JSONObject> getStudyFilesByCategory(String category, String search,int environment,int pageNum, int pageSize) {
        List<JSONObject> list = Lists.newArrayList();
        if(ObjectUtils.isEmpty(category)){
            List<String> treeList = Lists.newArrayList();
            List<HashMap<String, Object>> treeListByParentSerialNumber = studyFilesMapper.getStudyFilesCategoryChildIdList("");
            if (!ObjectUtils.isEmpty(treeListByParentSerialNumber)) {
                treeListByParentSerialNumber.forEach(nc -> {
                    treeList.add(nc.get("studyFilesCategorySerialNumber").toString());
                });
            }
            treeList.forEach(ca -> {
                PageHelper.startPage(pageNum, pageSize);
                List<StudyFiles> studyFilesByCategory = studyFilesMapper.getStudyFilesByCategory(ca,search,environment);
                PageInfo<StudyFiles> pageInfo = new PageInfo<>(studyFilesByCategory);
                JSONObject jsonObject = new JSONObject();
                HashMap<String, Object> childCategory = studyFilesMapper.getStudyFilesCategoryByCategorySerialNumber(ca);
                jsonObject.put("category", childCategory.get("studyFilesCategory").toString());
                jsonObject.put("studyFiles", pageInfo.getList());
                jsonObject.put("total", pageInfo.getTotal());
                if (!pageInfo.getList().isEmpty()) {
                    list.add(jsonObject);
                }
            });
        }else {
            HashMap<String, Object> studyFilesCategory = studyFilesMapper.getStudyFilesCategoryByCategorySerialNumber(category);
            if (!ObjectUtils.isEmpty(studyFilesCategory)) {
                List<String> treeList = Lists.newArrayList();
                treeList.add(studyFilesCategory.get("studyFilesCategorySerialNumber").toString());
                List<HashMap<String, Object>> treeListByParentSerialNumber = studyFilesMapper.getStudyFilesCategoryChildIdList(category);
                if (!ObjectUtils.isEmpty(treeListByParentSerialNumber)) {
                    treeListByParentSerialNumber.forEach(nc -> {
                        treeList.add(nc.get("studyFilesCategorySerialNumber").toString());
                    });
                }
                treeList.forEach(ca -> {
                    PageHelper.startPage(pageNum, pageSize);
                    List<StudyFiles> studyFilesByCategory = studyFilesMapper.getStudyFilesByCategory(ca, search,environment);
                    PageInfo<StudyFiles> pageInfo = new PageInfo<>(studyFilesByCategory);
                    JSONObject jsonObject = new JSONObject();
                    HashMap<String, Object> childCategory = studyFilesMapper.getStudyFilesCategoryByCategorySerialNumber(ca);
                    jsonObject.put("category", childCategory.get("studyFilesCategory").toString());
                    jsonObject.put("studyFiles", pageInfo.getList());
                    jsonObject.put("total", pageInfo.getTotal());
                    if (!pageInfo.getList().isEmpty()) {
                        list.add(jsonObject);
                    }
                });
            }
        }
        return list;

    }

    @Override
    public List<PersonalBookshelf> getPersonalBookshelf(String openId) {
        return studyFilesMapper.getPersonalBookshelf(openId);
    }

    @Override
    public List<PersonalBrowsing> getPersonalBrowsing(String openId) {
        return studyFilesMapper.getPersonalBrowsing(openId,"");
    }

    @Override
    public List<PersonalBrowsingDetail> getPersonalBrowsingDetail(String openId, String browsingSerialNumber) {
        return studyFilesMapper.getPersonalBrowsingDetail(openId,browsingSerialNumber);
    }

    @Override
    public JSONObject getMicroRedLookBook(String tags,String search, int environment,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<StudyFiles> StudyFilesByCategory = studyFilesMapper.getMicroRedLookBook(tags,search,environment);
        for (StudyFiles nc : StudyFilesByCategory) {
            if (nc.getStudyFilesPublishDatetime() != null) {
                try {
                    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    if (( new Date().getTime() - dateFormat.parse(nc.getStudyFilesPublishDatetime()).getTime())/(24*60*60*1000) <= 3) {
                        nc.setIfNew("1");
                    }else{
                        nc.setIfNew("0");
                    }
                } catch (ParseException e) {
                    nc.setIfNew("0");
                }
            }
        }
        PageInfo<StudyFiles> pageInfo = new PageInfo<>(StudyFilesByCategory);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public List<StudyFilesTags> getStudyFilesTagsList() {
        return studyFilesMapper.getStudyFilesTagsList();
    }

    @Override
    public JSONObject getMicroRedLookBookTagsList() {
        JSONObject jsonObject = new JSONObject();
        List<JSONObject> tagsList = Lists.newArrayList();
        JSONObject allTag = new JSONObject();
        allTag.put("title","全部");
        allTag.put("value","");
        tagsList.add(allTag);
        List<StudyFilesTags> studyFilesTagsList = studyFilesMapper.getStudyFilesTagsList();
        for (StudyFilesTags studyFilesTags : studyFilesTagsList) {
            JSONObject tag = new JSONObject();
            tag.put("title",studyFilesTags.getStudyFileTag());
            tag.put("value",studyFilesTags.getStudyFileTagSerialNumber());
            tagsList.add(tag);
        }
        jsonObject.put("tagsList",tagsList);
        return jsonObject;
    }
}
